package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.context.SpringContextUtils;
import com.cn.jdia.tas.model.T_employee;
import com.cn.jdia.tas.service.IEmployeeService;
import com.cn.jdia.tas.utils.PingyinUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class EmployeeController {

	@Resource
	private IEmployeeService service = null;
	
	@RequestMapping("/fuzzyqueryemployee")
	public String fuzzyQueryEmployeeData(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String name = pmap.get("name");
		System.out.println(name);		
		String pname="%"+name+"%";
		
//		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
//		service = (IEmployeeService) w_context.getBean("employeeService");
		
		service=(IEmployeeService)SpringContextUtils.getContext().getBean("employeeService");
		
		List<T_employee> employeeList = service.queryEmployeeByName(pname);
		
		List<Map<String,String>> list=new ArrayList<>();
		for(T_employee tmp:employeeList){
			String eid=tmp.getEmployeeid();
			String ename=tmp.getEmployeename();
			Map<String,String> map=new HashMap<>();
			map.put("eid", eid);
			map.put("ename", ename);
			list.add(map);
		}		
		
		out.write(gson.toJson(list));
		out.flush();
		out.close();
		return null;
	
	}
	
	@RequestMapping("/allemployee")
	public String allEmployeeData(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
//		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
//		service = (IEmployeeService) w_context.getBean("employeeService");
		
		service=(IEmployeeService)SpringContextUtils.getContext().getBean("employeeService");
		
		List<Map<String,String>> list=new ArrayList<>();
		List<T_employee> allEmployee = service.getAllEmployee();		
		for(T_employee tmp:allEmployee){			
			Map<String,String> map=new HashMap<>();
			map.put("eid", tmp.getEmployeeid());
			map.put("ename", tmp.getEmployeename());
			map.put("first",String.valueOf( PingyinUtils.getFirstSpell(tmp.getEmployeename()).charAt(0)));
			list.add(map);
		}
		
		String json = gson.toJson(list);
		out.write(json);
		out.flush();
		out.close();
		return null;
	}
	
	@RequestMapping("/contact")
	public String getContactData(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String id = pmap.get("id");
		System.out.println(id);
//		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
//		service = (IEmployeeService) w_context.getBean("employeeService");
		
		service=(IEmployeeService)SpringContextUtils.getContext().getBean("employeeService");

		Map<String,String> map=new HashMap<>();
		T_employee employee = service.getEmployeeInfoById(id);
		map.put("sex", employee.getSex());
		map.put("mobile", employee.getMobile());
		map.put("dept", employee.getDepatment().getDeptname());
		
		out.write(gson.toJson(map));
		out.flush();
		out.close();
		return null;
	}
}
